from tensorboard.backend.event_processing import event_accumulator
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib.ticker import LinearLocator, FormatStrFormatter, MultipleLocator

#加载日志数据
ea=event_accumulator.EventAccumulator(r'F:\Unity_Project\FlightAndDodgeProject\Assets\ML-Agents\Training\results\VisualHardRun\FlyAndDodge\events.out.tfevents.1622829115.DESKTOP-KNOOAL9.23280.0') 
ea.Reload()
print(ea.scalars.Keys())

reward_data = ea.scalars.Items('Environment/Cumulative Reward')
episode_length_data = ea.scalars.Items('Environment/Episode Length')
reward_step = [reward.step for reward in reward_data]
reward_value = [reward.value for reward in reward_data]
episode_length_step = [episode_length.step for episode_length in episode_length_data]
episode_length_value = [episode_length.value for episode_length in episode_length_data]


# 画图
label_font = FontProperties(fname=r"C:\windows\fonts\simsun.ttc", size=15)
tick_size = 12

xmajorLocator = MultipleLocator(1000000) # 将x主刻度标签设置为10的倍数
xmajorFormatter = FormatStrFormatter('%3.0f') # 设置x轴标签文本的格式
xminorLocator   = MultipleLocator(500000) # 将x轴次刻度标签设置为5的倍数  
ymajorLocator = MultipleLocator(0.2) # 将y轴主刻度标签设置为0.5的倍数
ymajorFormatter = FormatStrFormatter('%0.1f') # 设置y轴标签文本的格式
yminorLocator   = MultipleLocator(0.1) # 将此y轴次刻度标签设置为0.1的倍数  

plt.figure(figsize=(10,5))
ax = plt.subplot(1,2,1)
plt.plot(reward_step, reward_value)
plt.xlabel('与环境交互次数',fontproperties=label_font)
plt.ylabel('累计奖励值',fontproperties=label_font)
plt.xticks(fontsize=tick_size)
plt.yticks(fontsize=tick_size)
plt.minorticks_on()
plt.xlim(left=0)
plt.ylim(top=0.4, bottom=-1.2)
ax.xaxis.set_major_locator(xmajorLocator)  # 设置x轴主刻度
# ax.xaxis.set_major_formatter(xmajorFormatter)  # 设置x轴标签文本格式
ax.xaxis.set_minor_locator(xminorLocator)  # 设置x轴次刻度
ax.yaxis.set_major_locator(ymajorLocator)  # 设置y轴主刻度
# ax.yaxis.set_major_formatter(ymajorFormatter)  # 设置y轴标签文本格式
ax.yaxis.set_minor_locator(yminorLocator)  # 设置y轴次刻度
# ax.xaxis.grid(True, linestyle = "-",which='major') #x坐标轴的网格使用主刻度
# ax.yaxis.grid(True, linestyle = "-",which='major') #y坐标轴的网格使用次刻度
# ax.ticklabel_format(axis='y', style='scientific', scilimits=(0,0))
plt.grid()


ax = plt.subplot(1,2,2)
plt.plot(episode_length_step, episode_length_value)
plt.xlabel('与环境交互次数',fontproperties=label_font)
plt.ylabel('幕长度',fontproperties=label_font)
plt.xticks(fontsize=tick_size)
plt.yticks(fontsize=tick_size)
plt.minorticks_on()
plt.grid()
plt.show()
# plt.savefig('VisualHardRun',dpi=300)



